function isEmpty(obj) {
    if (obj == null || obj == undefined || obj == '' || $.isEmptyObject(obj)) {
        return true
    }
    return false
}

function getImg(icons, iconkey) {
    const nodeIcon = icons[iconkey];
    if (!isEmpty(nodeIcon)) {
        if (nodeIcon.imageformat) {
            return 'data:' + nodeIcon.imageformat + ';base64,' + nodeIcon.image
        } else {
            return 'data:image/png;base64,' + nodeIcon.image
        }
    } else {
        return defaultNodeImg
    }
}
export function createNode(ele, topoIconData, defaultNodeImg) {
    let node = new ht.Node(); //创建网元,设置id
    node.setId(ele.selementid);
    node.setAttr("data", ele); //网元的各种数据都存入网元中
    node.setPosition(parseFloat(ele.x ? ele.x : 100), parseFloat(ele.y ? ele.y : 100));
    node.setToolTip(ele.label);
    // node.setSize(parseInt(ele.width ? ele.width : 20), parseInt(ele.height ? ele.height : 20));
    node.setSize(20, 20);
    node.setStyle("pixelPerfect", false); //图元在 2d 下，透明区域是否忽略选中图元，默认为 true，表示忽略选中
    node.setStyle("label.align", "center");
    node.setStyle("label.font", "8px arial");
    node.setName(ele.label);
    if (!isEmpty(ele.iconid_off)) {
        node.setImage(getImg(topoIconData, ele.iconid_off))
    }
    return node
}
export function createLink(ele, source, target) {
    let link = new ht.Edge(); //创建线,设置id
    link.setId(ele.linkid);
    link.setAttr("data", ele); //线的各种数据都存入线中
    link.setSource(source);
    link.setTarget(target);
    //设置图层的样式
    link.setStyle("edge.color", '#' + (ele.color ? ele.color : '000'));
    link.setStyle('edge.width', 1);
    link.setName(ele.label);
    // link.setStyle("label.align", "left");
    // link.setStyle("label.offset.x", 0);
    // link.setStyle("label2.position", 3);

    return link
}
export default {
    setTopoStyle(data, graphView, topoIconData) {
        let dataModel = graphView.getDataModel();
        //注册全局图片 arrow 箭头
        ht.Default.setImage('arrow', {
            width: 60,
            height: 50,
            comps: [{
                type: 'shape',
                points: [15, 5, 30, 25, 15, 45, 50, 25, 15, 5],
                background: 'black',
                borderWidth: 1,
                borderColor: 'white'
            }]
        });
        // 遍历点 向拓扑图添加点
        $.each(data.selements, function() {
            let node = createNode(this, topoIconData)
            dataModel.add(node); //添加到网元容器中
        });
        // 遍历线 向拓扑图添加线
        $.each(data.links, function() {
            let source = dataModel.getDataById(this.selementid1)
            let target = dataModel.getDataById(this.selementid2)
            let link = createLink(this, source, target)
            dataModel.add(link); //添加到网元容器中
        });
    },
    updateInOrOutData(graphView, hostidAndInOrOutData) {
        let dataModel = graphView.getDataModel();
        let allElements = dataModel.getDatas();
        allElements.each(function(link) {
            if (!(link instanceof ht.Edge)) {
                return true;
            }
            const targetNode = link.getTarget();
            // 动态流量
            let kb1 = "",
                kb2 = "";
            if (hostidAndInOrOutData != null) {
                const targetAttrObj = targetNode._attrObject;
                if (targetAttrObj != undefined && targetAttrObj != null) {
                    const targetAttrData = targetAttrObj.data;
                    if (targetAttrData != undefined && targetAttrData != null) {
                        const targetEelementId = targetAttrData.elementid;
                        const inOroutData = hostidAndInOrOutData[targetEelementId];
                        // console.log('inoroutData', targetEelementId, inOroutData);
                        if (inOroutData != undefined && inOroutData != null) {
                            kb1 = inOroutData.incoming ? inOroutData.incoming : 0;
                            kb2 = inOroutData.outgoing ? inOroutData.outgoing : 0;
                        }
                    }
                }

                // 正常
                // var dashColor = '#13CE66'; // 绿
                let dashColor = ''; // 绿
                // 大于5G流量
                if (kb2 > 5000000000) {
                    dashColor = '#FF4949'; // 红
                    // 大于3G流量
                } else if (kb2 > 3000000000) {
                    dashColor = '#F7BA2A'; // 黄
                    // 大于1G流量
                } else if (kb2 > 1000000000) {
                    dashColor = '#20A0FF'; // 蓝
                }
                link.s({
                    'edge.width': 2,
                    'edge.gap': 20,
                    'edge.dash': true,
                    'edge.dash.color': dashColor,
                    'edge.offset': 0,
                    'select.color': 'yellow',
                    'label': (kb1 / 1024 / 1024).toFixed(2) + 'Mbps',
                    'label.position': 54,
                    'label.font': '5px arial',
                    'label.offset.x': 0,
                    'label.offset.y': 2,
                    'label2': (kb2 / 1024 / 1024).toFixed(2) + 'Mbps',
                    'label2.position': 39,
                    'label2.font': '5px arial',
                    'label2.offset.x': 0,
                    'label2.offset.y': -2,
                    'icons': {
                        fromArrow: {
                            position: 45,
                            names: ['arrow'],
                            rotation: Math.PI,
                            keepOrien: true,
                            width: 15,
                            height: 7
                        },
                        toArrow: {
                            position: 48,
                            names: ['arrow'],
                            keepOrien: true,
                            width: 15,
                            height: 7
                        }
                    }
                });
            }
        });

    }
}
